<template>
  <!--财务管理-->
  <div class="statistics-container">
    <vab-query-form>
      <vab-query-form-left-panel :span="2">
        <el-button type="primary" icon="el-icon-plus" @click="add()">
          新增
        </el-button>
      </vab-query-form-left-panel>
      <vab-query-form-right-panel :span="22">
        <el-form :inline="true" :model="queryForm" @submit.native.prevent>
<!--          <el-form-item>
            <div style="width: 80px">优惠券名称:</div>
          </el-form-item>-->
          <el-form-item>
            <el-input
              v-model.trim="queryForm.name"
              style="width: 180px"
              placeholder="请输入优惠券名称"
              clearable
            />
          </el-form-item>
<!--          <el-form-item>
            <div style="width: 80px">收取人：</div>
          </el-form-item>-->
          <el-form-item>
            <el-select
              v-model="queryForm.user"
              clearable
              placeholder="请选择收取人"
              style="width: 180px"
            >
              <el-option
                v-for="item in user"
                :key="item.id"
                :label="item.company_name"
                :value="item.id"
              />
            </el-select>
          </el-form-item>
<!--          <el-form-item>
            <div style="width: 60px">状态：</div>
          </el-form-item>-->
          <el-form-item>
            <el-select
              v-model="queryForm.status"
              clearable
              placeholder="请选择状态"
              style="width: 180px"
            >
              <el-option
                v-for="item in status"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </el-form-item>
<!--          <el-form-item>
            <div style="width: 80px">截止日期：</div>
          </el-form-item>-->
          <el-form-item>
            <el-date-picker
              v-model="queryForm.date"
              type="daterange"
              value-format="yyyy-M-d"
              range-separator="-"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
              style="width:250px;"
            />
          </el-form-item>
          <el-form-item>
            <el-button type="primary" icon="el-icon-search" @click="search">
              查询
            </el-button>
          </el-form-item>
          <el-form-item>
            <el-button
              type="primary"
              icon="el-icon-refresh-right"
              @click="restart"
            >
              重置
            </el-button>
          </el-form-item>
        </el-form>
      </vab-query-form-right-panel>
    </vab-query-form>

    <el-table
      v-loading="listLoading"
      :data="list"
      @selection-change="setSelectRows"
    >
      <el-table-column
        align="center"
        show-overflow-tooltip
        label="序号"
        sortable
        type="index"
      />
      <el-table-column
        align="center"
        show-overflow-tooltip
        prop="coupon_name"
        label="优惠券名称"
      />
      <el-table-column
        align="center"
        show-overflow-tooltip
        prop="expiration_date"
        label="截止日期"
      />
      <el-table-column
        align="center"
        show-overflow-tooltip
        prop="create_by"
        label="发送人"
      />
      <el-table-column
        align="center"
        show-overflow-tooltip
        prop="user_company_name"
        label="收取人"
      />
      <el-table-column
        align="center"
        show-overflow-tooltip
        prop="money"
        label="金额"
      />
      <el-table-column
        align="center"
        show-overflow-tooltip
        prop="number"
        label="数量"
      />
      <el-table-column
        align="center"
        show-overflow-tooltip
        prop="send_status_text"
        label="状态"
      />
      <el-table-column align="center" label="操作">
        <template slot-scope="scope">
          <el-button
            style="font-size: 14px; margin-right: 20px"
            type="text"
            @click="edit(scope)"
          >
            编辑
          </el-button>
          <el-dropdown style="color: #1890ff">
            <span class="el-dropdown-link">
              更多
              <i class="el-icon-arrow-down el-icon--right"></i>
            </span>
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item
                v-show="scope.row.send_status == 0"
                @click.native="sendCou(scope.row)"
              >
                发送
              </el-dropdown-item>
              <el-dropdown-item @click.native="delList(scope.row)">
                删除
              </el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
        </template>
      </el-table-column>
    </el-table>

    <!-- 弹窗 -->
    <el-dialog :title="wintit" :visible.sync="dialogFormVisible" width="30%">
      <el-form :model="form" label-width="120px">
        <el-form-item label="优惠价名称:" required>
          <el-input v-model="form.coupon_name" placeholder="请优惠价名称" />
        </el-form-item>
        <el-form-item label="截止日期:" required>
          <el-date-picker
            v-model="form.expiration_date"
            type="date"
            placeholder="选择开始时间"
          />
        </el-form-item>
        <el-form-item label="金额:">
          <el-input v-model="form.money" placeholder="请输入金额" />
        </el-form-item>
        <el-form-item label="数量:">
          <el-input v-model="form.number" placeholder="请输入数量" />
        </el-form-item>
        <el-form-item label="收取人:" required>
          <el-select
            v-model="form.user_account_id"
            clearable
            placeholder="请选择"
          >
            <el-option
              v-for="item in user"
              :key="item.id"
              :label="item.company_name"
              :value="item.id"
            />
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button v-show="addtype" type="primary" @click="send()">
          确 定
        </el-button>
        <el-button v-show="edittype" type="primary" @click="save()">
          确 定
        </el-button>
      </div>
    </el-dialog>

    <el-pagination
      background
      :current-page="queryForm.page"
      :page-size="queryForm.list_row"
      :layout="layout"
      :total="total"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    />
  </div>
</template>

<script>
  import {
    getCouponLists,
    getBusinessUserLists,
    saveCoupons,
    sendCoupons,
    delCoupons,
  } from '@/api/common'

  export default {
    name: 'Coupon',
    data() {
      return {
        user: [],
        status: [
          {
            value: 0,
            label: '未发送',
          },
          {
            value: 1,
            label: '已发送',
          },
        ],
        queryForm: {
          account: '',
          name: '',
          user: '',
          status: '',
          date: '',
        },
        form: {
          coupon_name: '',
          expiration_date: '',
          money: '',
          number: '',
          user_account_id: '',
        },
        disabled: false,
        listLoading: false,
        list: [],
        layout: 'total, sizes, prev, pager, next, jumper',
        total: 0,
        dialogFormVisible: false,
        wintit: '',
        coId: '',
        addtype: false,
        edittype: false,
      }
    },
    created() {
      this.getData()
      this.getUser()
    },
    methods: {
      handleDetail() {},
      //搜索
      search() {
        this.getData()
      },
      restart() {
        this.queryForm = []
        this.queryForm.date = ''
        this.getData()
      },
      //导出
      handleExport() {},
      getData() {
        let params = {
          page: this.queryForm.page,
          limit: this.queryForm.list_row,
          expiration_date_begin: this.queryForm.date[0],
          expiration_date_end: this.queryForm.date[1],
          user_account_id: this.queryForm.user,
          send_status: this.queryForm.status,
          coupon_name: this.queryForm.name,
        }
        getCouponLists(params).then((data) => {
          this.list = data.data
          this.total = data.count
        })
      },
      getUser() {
        let params = {
          page: 1,
          limit: 100000,
          // search: this.queryForm.keyword,
        }
        getBusinessUserLists(params).then((data, count) => {
          this.user = data.data
        })
      },
      add() {
        this.dialogFormVisible = true
        this.wintit = '添加'
        this.addtype = true
        this.edittype = false
        this.form = []
      },
      edit(row) {
        this.dialogFormVisible = true
        this.wintit = '编辑'
        this.addtype = false
        this.edittype = true
        this.form = this.list[row.$index]
        this.coId = row.row.id
      },
      send() {
        let params = {
          coupon_name: this.form.coupon_name,
          expiration_date: this.form.expiration_date,
          money: this.form.money,
          number: this.form.number,
          user_account_id: this.form.user_account_id,
        }
        saveCoupons(params).then((data) => {
          this.$message.success('添加成功！')
          this.dialogFormVisible = false
          this.getData()
        })
      },
      save() {
        let params = {
          id: this.coId,
          coupon_name: this.form.coupon_name,
          expiration_date: this.form.expiration_date,
          money: this.form.money,
          number: this.form.number,
          user_account_id: this.form.user_account_id,
        }
        saveCoupons(params).then((data) => {
          this.$message.success('修改成功！')
          this.dialogFormVisible = false
          this.getData()
        })
      },
      delList(row) {
        let params = {
          id: row.id,
        }
        delCoupons(params).then((data) => {
          this.$message.success('删除成功！')
          this.getData()
        })
      },
      sendCou(row) {
        let params = {
          id: row.id,
        }
        sendCoupons(params).then((data) => {
          this.$message.success('发送成功！')
          this.getData()
        })
      },
      setSelectRows() {},
      handleSizeChange(val) {
        this.queryForm.list_row = val
        this.getData()
      },
      handleCurrentChange(val) {
        this.queryForm.page = val
        this.getData()
      },
    },
  }
</script>

<style lang="scss" scoped>
  .el-button {
    font-size: 14px;
    margin-right: 20px;
  }
</style>
